package com.exam.bean.privilege;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;

/**
 * 权限
 *
 */
@Entity
public class SystemPrivilege implements Serializable{
	
	private static final long serialVersionUID = -8436059251218624645L;
	
	private SystemPrivilegePK id;//标识
	private String name;//权限名称
	private String description;//权限描述
	private Set<PrivilegeGroup> privilegegroups = new HashSet<PrivilegeGroup>();//所在权限组
	
	
	public SystemPrivilege(){}
	
	public SystemPrivilege(SystemPrivilegePK id) {
		this.id = id;
	}
	
	public SystemPrivilege(SystemPrivilegePK id, String name,String description) {
		this.id = id;
		this.name = name;
		this.description=description;
	}
	
	public SystemPrivilege(String model, String privilegeValue, String name,String description) {
		this.id = new SystemPrivilegePK(model, privilegeValue);
		this.name = name;
		this.description=description;
	}
	
	
	@EmbeddedId
	public SystemPrivilegePK getId() {
		return id;
	}
	public void setId(SystemPrivilegePK id) {
		this.id = id;
	}
	
	@Column(length=30,nullable=false)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@Column(length=30)
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}

	@ManyToMany(mappedBy="privileges", cascade=CascadeType.REFRESH)
	public Set<PrivilegeGroup> getPrivilegegroups() {
		return privilegegroups;
	}
	public void setPrivilegegroups(Set<PrivilegeGroup> privilegegroups) {
		this.privilegegroups = privilegegroups;
	}
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final SystemPrivilege other = (SystemPrivilege) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
}
